#include "PauseState.h"
#include "..\Managers\MenuManager.h"
#include "..\GUI\TextButton.h"
#include "..\Define.h"
#include <functional>
#include "..\Managers\StateManager.h"
#include "MenuState.h"
#include "..\Managers\GameObjectManager.h"
#include "..\rapidxml.hpp"
#include "..\rapidxml_print.hpp"
#include "..\Components\ComponentVisitor.h"
#include "..\Components\GameObject.h"
#include "..\Components\PhysicsComponent.h"
#include "..\Components\PlayerHealthComponent.h"
#include <fstream>
#include "..\Generic\Convert.h"
#include "..\Managers\PhysicsManager.h"
#include "..\Generic\KillCounter.h"

PauseState::PauseState(GameObjectManager* p_gameObjectManager, KillCounter* p_killCounter) : m_gameObjectManager(p_gameObjectManager), m_kill_counter(p_killCounter){}
PauseState::~PauseState(void){}

void PauseState::initialize()
{
	m_menuManager = new MenuManager(vn::Vector2(VSW_05, VSH_05), 30.f);
	TextButton* button = new TextButton("Continue", "mm_font", vn::Color::white, vn::Color::yellow, 2);
	std::function<void()> func = [=] { StateManager::getSingleton().popState(); };
	button->registerFunctionCallback(func);
	m_menuManager->addChoice(button);
	button = new TextButton("Save Game", "mm_font", vn::Color::white, vn::Color::yellow, 2);
	func = [this] { saveGame(); };
	button->registerFunctionCallback(func);
	m_menuManager->addChoice(button);
	button = new TextButton("Quit", "mm_font", vn::Color::white, vn::Color::yellow, 2);
	func = [=] { StateManager::getSingleton().popState(); StateManager::getSingleton().setState(new MenuState); };
	button->registerFunctionCallback(func);
	m_menuManager->addChoice(button);
	m_menuManager->setCurrentChoice(0);
	m_menuManager->setActive(true);
	m_menuManager->addTitle(2, "Pause Menu", "mm_font", vn::Color::green, 1.0f, 1.0f);
	PhysicsManager::getSingleton().setPause(true);
}

void PauseState::shutdown()
{
	PhysicsManager::getSingleton().setPause(false);
	delete m_menuManager;
	m_menuManager = nullptr;
}

void PauseState::update(const GameTime& p_gameTime)
{
	m_menuManager->update();
}

void PauseState::handleEvents()
{

}


void PauseState::saveGame()
{
	ComponentVisitor visitor;
	GameObject* go = m_gameObjectManager->getGameObject("player");
	PlayerHealthComponent* phc = static_cast<PlayerHealthComponent*>(go->getComponent("HealthComponent"));
	PhysicsComponent* pc = static_cast<PhysicsComponent*>(go->getComponent("PhysicsComponent"));
	pc->acceptVisitor(&visitor);
	phc->acceptVisitor(&visitor);
	m_kill_counter->acceptVisitor(&visitor);

	std::string posX = numberToString(visitor.getPosition().x);
	std::string posY = numberToString(visitor.getPosition().y);
	std::string hp = numberToString(visitor.getHealth());
	std::string kill_count = numberToString(visitor.getKillCount());

	rapidxml::xml_document<> doc;
	rapidxml::xml_node<>* decl = doc.allocate_node(rapidxml::node_declaration);
    decl->append_attribute(doc.allocate_attribute("version", "1.0"));
    decl->append_attribute(doc.allocate_attribute("encoding", "UTF-8"));
    doc.append_node(decl);

	rapidxml::xml_node<>* saves = doc.allocate_node(rapidxml::node_element, "player");
    doc.append_node(saves);

	rapidxml::xml_attribute<>* x = doc.allocate_attribute("x", posX.c_str());
	saves->append_attribute(x);
	rapidxml::xml_attribute<>* y = doc.allocate_attribute("y", posY.c_str());
	saves->append_attribute(y);

	rapidxml::xml_attribute<>* health = doc.allocate_attribute("health", hp.c_str());
	saves->append_attribute(health);

	rapidxml::xml_attribute<>* killCount = doc.allocate_attribute("killcount", kill_count.c_str());
	saves->append_attribute(killCount);

	std::ofstream file;
	file.open("saves\\save.xml");
	file << doc;
	file.close();
}